$(function () {
    $("#jqGrid").jqGrid({
        url: '../recruitment/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true, align: "center"},
            {label: '职位名称', name: 'jobName', index: 'job_name', width: 80, align: "center"},
            // {label: '职位描述', name: 'desc', index: 'desc', width: 80, align: "center"},
            // {label: '岗位职责1', name: 'res1', index: 'res1', width: 80, align: "center"},
            // {label: '岗位职责2', name: 'res2', index: 'res2', width: 80, align: "center"},
            // {label: '岗位职责3', name: 'res3', index: 'res3', width: 80, align: "center"},
            // {label: '岗位职责4', name: 'res4', index: 'res4', width: 80, align: "center"},
            // {label: '岗位职责5', name: 'res5', index: 'res5', width: 80, align: "center"},
            // {label: '岗位职责6', name: 'res6', index: 'res6', width: 80, align: "center"},
            // {label: '职位要求', name: 'qualifications', index: 'qualifications', width: 80, align: "center"},
            // {label: '职位待遇', name: 'treatment', index: 'treatment', width: 80, align: "center"},
            {
                label: '新增时间', name: 'addTime', index: 'add_time', width: 80, align: "center",
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
            {
                label: '更新时间', name: 'updateTime', index: 'update_time', width: 80, align: "center",
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }],
        viewrecords: true,
        height: 495,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $('#qualifications').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '300px', //高度
        minHeight: '100px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    });
    $('#treatment').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '300px', //高度
        minHeight: '100px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })
    $('#desc').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '300px', //高度
        minHeight: '100px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        recruitment: {},
        ruleValidate: {
            jobName: [
                {required: true, message: '职位名称不能为空', trigger: 'blur'}
            ],
            desc: [
                {required: true, message: '职位描述不能为空', trigger: 'blur'}
            ],
            res1: [
                {required: true, message: '岗位职责1不能为空', trigger: 'blur'}
            ],
            res2: [
                {required: true, message: '岗位职责2不能为空', trigger: 'blur'}
            ],
            res3: [
                {required: true, message: '岗位职责3不能为空', trigger: 'blur'}
            ],
            qualifications: [
                {required: true, message: '任职资格不能为空', trigger: 'blur'}
            ],
            treatment: [
                {required: true, message: '福利待遇不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.recruitment = {};
            $('#qualifications').editable('setHTML', '');
            $('#treatment').editable('setHTML', '');
            $('#desc').editable('setHTML', '');

        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.recruitment.id == null ? "../recruitment/save" : "../recruitment/update";
            vm.recruitment.qualifications = $('#qualifications').editable('getHTML');
            vm.recruitment.treatment = $('#treatment').editable('getHTML');
            vm.recruitment.desc = $('#desc').editable('getHTML');


            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.recruitment),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../recruitment/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../recruitment/info/" + id, function (r) {
                vm.recruitment = r.recruitment;
                $('#qualifications').editable('setHTML', vm.recruitment.qualifications);
                $('#treatment').editable('setHTML', vm.recruitment.treatment);
                $('#desc').editable('setHTML', vm.recruitment.desc);

            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});